package model

import (
	"errors"

	"gorm.io/gorm"
)

type CommentLike struct {
	CommentID int            `gorm:"not null"`
	UserID    int            `gorm:"not null"`
	DeletedAt gorm.DeletedAt `gorm:"index"`
	Comment   *Comment       `gorm:"foreignKey:CommentID"`
	User      *User          `gorm:"foreignKey:UserID"`
}

func (c *CommentLike) SaveCommentLike() (*CommentLike, error) {
	err := DB.Create(&c).Error
	if err != nil {
		return &CommentLike{}, err
	}
	return c, nil
}

func (c *CommentLike) DeleteCommentLike() (*CommentLike, error) {
	err := DB.Where("CommentID = ? AND UserID = ?", c.CommentID, c.UserID).Delete(&c).Error
	if err != nil {
		return &CommentLike{}, err
	}
	return c, nil
}
//根据评论的ID获取评论的点赞数量
func GetCommentLikeCount(commentID int) (int, error) {
	var countCommentLike []CommentLike
	err := DB.Where("CommentID = ?", commentID).Find(&countCommentLike).Error
	count := len(countCommentLike)
	if err != nil {
		return 0, err
	}
	return  count, nil
}
func GetisLikeComment(commentID int , userID int)(bool , error){
	var comment CommentLike
	err := DB.Where("CommentID = ? and UserID = ?",commentID,userID).Find(&comment).Error
	if err != nil{
		if errors.Is(err, gorm.ErrRecordNotFound) {
			// 查询结果为空
			return false, nil
		}
		return false, err
	}
	if comment.CommentID ==0 {
		return false, nil                                    
	}
	return true, nil

}
